layui.use(['laypage', 'table', 'form', 'laydate'], function(){
    var table = layui.table,laydate = layui.laydate,util = layui.util,form = layui.form,element = layui.element;

    let todoCols = [
        [
            {field: 'id', title: '标识', width:80, align: 'center', hide: true}
            ,{field: 'code', title: '配合比编号', width:240, align: 'center',sort:true}
            ,{field: 'labLevelCode', title: '标准配比编号', width:220, align: 'center',sort:true}
            ,{field: 'mixTime', title: '搅拌时间', width: 80, align: 'center',sort:true}
            ,{field: 'dosage', title: '容重', width: 100, align: 'center',sort:true, templet:function (val){
                var dosage = val.dosage;
                if (dosage != null)
                    return '<label style="color: #dc322f;">' + val.dosage + '</label>';
                return '';
            }}
            ,{field: 'lineName', title: '生产线', width: 100, align: 'center',sort:true}
            ,{field: 'isUpload', title: '状态', width:80, align: 'center',sort:true, templet:function (val){
                var isUpload = val.isUpload;
                if (isUpload == 0)
                    return '未上传';
                return '已上传';
            }}
            ,{field: 'createTime', title: '上传时间', width:160, align: 'center'}
            ,{field: 'creator', title: '操作人', width:100, align: 'center'}
            ,{field: 'remark', title: '备注', width: 200, align: 'center'}
        ]
    ];

    table.render({
        elem: '#body-list'
        ,url: '/lab/expend-upload/find-history'
        ,method: 'post'
        ,page: true
        ,cols: todoCols
        ,maxHeight:300
        ,where: {
            indentId: $('.indentId').val()
        }
        ,parseData: function(res){ //res 即为原始返回的数据
            // console.log(res)
            if (res.code == 0 && res.data != null) {
                return {
                    "code": res.code, //解析接口状态
                    "msg": res.msg, //解析提示文本
                    "count": 0, //解析数据长度
                    "data": res.data //解析数据列表
                };
            } else {
                return {
                    "code": res.code, //解析接口状态
                    "msg": '没有任何数据', //解析提示文本
                    "count": 0, //解析数据长度
                    "data": '' //解析数据列表
                };
            }
        }
    });

    //监听行工具事件
    table.on('row(body-list)', function(obj){
        var data = obj.data;
        // loadData({indentId:data.indentId}, '/pro/indent/detail/' + data.indentId, 0);
        setTimeout(function () {
            loadData({uploadId:data.id}, '/lab/expend-upload/find-material', 1);
        }, 100);
        setTimeout(function () {
            loadData({constructId:data.labLevelConstructId}, '/lab/level-construct/find-detail', 2)
        }, 200);
    });

    function loadData(option, url, type) {
        // var loading = layer.load(1);
        $.ajax({
            url: url,
            type: 'post',
            dataType: 'json',
            data:option,
            timeout: 30000,
            success: function(res) {
                if(res.code == 0 && res.data != null) {
                    if (type == 0) {
                        var data = res.data;
                        buildIndent(data);
                    } else if (type == 1) {
                        var data = res.data;
                        buildMaterial(data);
                    } else if (type == 2) {
                        var data = res.data;
                        buildConstruct(data);
                    }
                } else {
                    layer.msg(res.msg);
                    return;
                }
            },
            error: function(res) {
                layer.msg("请求失败，请稍后再试！");
                return;
            },
            complete: function(XMLHttpRequest, status) {
                if(status == 'timeout') {
                    layer.msg("请求超时，请稍后再试！");
                    return;
                }
            }
        })
    }
    function buildConstruct(data) {
        if (data.code != null)
            $('.add-code').val(data.code);
        if (data.optimizecode != null)
            $('.add-optimize').val(data.optimizecode);
        if (data.labCode != null)
            $('.add-labCode').val(data.labCode);
        if (data.takeEffectDate != null)
            $('.add-takeEffectDate').val(data.takeEffectDate);
        if (data.mixRatio != null)
            $('.add-mixRatio').val(data.mixRatio);
        if (data.mixTime != null)
            $('.add-mixTime').val(data.mixTime);
    }
    function buildMaterial(data) {
        if (data != null) {
            var html = '';
            var sumConstruct = 0;
            var sumRealDosage = 0;
            $.each(data, function (index, val) {
                var materialType = convertVal(val.materialTypeName);
                var materialName = convertVal(val.materialName);
                var constructNum = 0;
                var standNum = 0;
                var rate = 0;
                var dosage = 0;
                var realDosage = 0;//实际用量
                if (val.constructNum != null) {
                    constructNum = parseFloat(val.constructNum).toFixed(2);
                    sumConstruct = parseFloat(sumConstruct) + parseFloat(constructNum);
                }
                if (val.standNum != null)
                    standNum = parseFloat(val.standNum).toFixed(2);
                if (val.rate != null)
                    rate = parseFloat(val.rate).toFixed(2);
                if (val.dosage != null)
                    dosage = parseFloat(val.dosage).toFixed(2);
                if (val.realDosage != null) {
                    realDosage = parseFloat(val.realDosage).toFixed(2);//实际用量
                    sumRealDosage = parseFloat(sumRealDosage) + parseFloat(realDosage);
                }
                var erpStorage = val.erpStorage;//ERP仓位
                var controlStorage = val.controlStorage;//工控仓位
                html += '<div class="layui-form-item material-item" style="margin-bottom: 5px;">\n' +
                    '<div class="layui-input-inline" style="width: 140px;">\n' +
                    '<input type="text" value="' + materialType + '" readonly class="layui-input add-materialType" placeholder="材料类型">\n' +
                    '</div>\n' +
                    '<div class="layui-input-inline" style="width: 220px;">\n' +
                    '<input type="text" value="' + materialName + '" readonly class="layui-input add-material" placeholder="材料">\n' +
                    '</div>\n' +
                    '<div class="layui-input-inline" style="width: 100px;">\n' +
                    '<input type="text" value="' + constructNum + '" readonly class="layui-input add-constructNum" placeholder="设计用量">\n' +
                    '</div>\n' +
                    '<div class="layui-input-inline" style="width: 100px;">\n' +
                    '<input type="text" value="' + standNum + '" readonly class="layui-input add-standNum" placeholder="砂浆用量">\n' +
                    '</div>\n' +
                    '<div class="layui-input-inline" style="width: 100px;">\n' +
                    '<input type="text" value="' + rate + '" readonly class="layui-input add-rate" placeholder="含水(%)">\n' +
                    '</div>\n' +
                    '<div class="layui-input-inline" style="width: 100px;">\n' +
                    '<input type="text" value="' + dosage + '" readonly class="layui-input add-dosage" placeholder="计算用量">\n' +
                    '</div>\n' +
                    '<div class="layui-input-inline" style="width: 100px;">\n' +
                    '<input type="text" style="color: #dc322f;" value="' + realDosage + '" readonly class="layui-input add-realDosage" placeholder="实际用量">\n' +
                    '</div>' +
                    '<div class="layui-input-inline" style="width: 220px;">\n' +
                    '<input type="text" value="' + erpStorage + '" readonly class="layui-input add-storage" placeholder="ERP仓储位置">\n' +
                    '</div>\n' +
                    '<div class="layui-input-inline" style="width: 220px;">\n' +
                    '<input type="text" value="' + controlStorage + '" readonly class="layui-input add-controlStorage" placeholder="工控仓储位置">\n' +
                    '</div></div>';
            });
            $('.add-setDosage').val(parseFloat(sumConstruct).toFixed(2));
            $('.add-realDosage').val(parseFloat(sumRealDosage).toFixed(2));
            $('.material-body-list').html(html);
        }
    }
    function buildIndent(data) {
        if (data.strengthName != null)
            $('.add-strengthName').val(data.strengthName);
        if (data.code != null)
            $('.add-indentCode').val(data.code);
        if (data.clientName != null)
            $('.add-clientName').val(data.clientName);
        if (data.site != null)
            $('.add-site').val(data.site);
        if (data.conName != null)
            $('.add-conName').val(data.conName);
        if (data.distance != null)
            $('.add-distance').val(data.distance);
        if (data.castModelName != null)
            $('.add-castModelName').val(data.castModelName);
        if (data.receiptUnit != null)
            $('.add-receiptUnit').val(data.receiptUnit);
        if (data.tech != null)
            $('.add-tech').val(data.tech);
        if (data.slumps != null)
            $('.add-slumps').val(data.slumps);
        if (data.dispatchStartTime != null)
            $('.add-dispatchStartTime').val(data.dispatchStartTime);
        if (data.actualStartTime != null)
            $('.add-actualStartTime').val(data.actualStartTime);
    }
});